#include "../include/socket.h"
#include "../include/macro.h"
#include <unistd.h>
#include <stdio.h>
void _socket::all_source_init()
{
    
    //初始化各种互斥量------这里直接默认不会出错
    pthread_mutex_init(&this->connection_mtx,NULL);
    pthread_mutex_init(&this->recycle_connection_mtx,NULL);
    pthread_mutex_init(&this->send_message_mutex,NULL);
    if(this->is_start_kick_map){
        pthread_mutex_init(&this->kick_mutex,NULL);
        this->kick_map_check_thread_running_flag=PRO_RUNNING;
    }
    this->send_message_thread_running_flag=PRO_RUNNING;
    
    this->deal_recycle_running_flag=PRO_RUNNING;
    // 初始化信号量
    
    pthread_cond_init(&this->send_thread_cond,0);
    //初始化线程---目前有发送消息线程函数------这个改了，不要去用信号量
    this->send_message_item=new thread_item_for_socket(this);
    pthread_create(&send_message_item->handler,NULL,send_message_to_client_thread,send_message_item);
    usleep(100*1000);   //一定程度保证该线程启动了
   

    //回收队列线程
    this->recycle_connection_item=new thread_item_for_socket(this);
    pthread_create(&recycle_connection_item->handler,NULL,deal_recycle_connection_thread,recycle_connection_item);
    usleep(100*1000);   //一定程度保证该线程启动了

    //踢人时钟线程
    if(this->is_start_kick_map){
        this->kick_map_item=new thread_item_for_socket(this);
        pthread_create(&kick_map_item->handler,NULL,kick_map_check_timeOut_thread,kick_map_item);
        usleep(100*1000);   //一定程度保证该线程启动了
    }
    
    printf("各种线程启动完毕\n");


}



void _socket::all_source_release()
{   
    
    

    // //资源应该不用这样回收了----这个变成多余的了
    // pro_status=PRO_END; //标志结束
    // //必须先回收线程，再销毁相关变量
    // sem_post(&this->send_message_sem);  //必须先post一个信号量，不然发送数据的线程永远退出不了，一直卡在这里
    // while(!function_threads.empty()){
    //     thread_item_for_socket * item=function_threads.front();
    //     function_threads.pop_front();
    //     pthread_join(item->handler,NULL);    //等待结束
    // }

    //这里进行一些变量的销毁即可
    pthread_mutex_destroy(&this->connection_mtx);
    pthread_mutex_destroy(&this->recycle_connection_mtx);
    pthread_mutex_destroy(&this->send_message_mutex);
    if(this->is_start_kick_map){
        pthread_mutex_destroy(&this->kick_mutex);
    }
    

    pthread_cond_destroy(&this->send_thread_cond);
    
    
    

}